#include <stdio.h>   
#include "stdlib.h"   
#include "string.h"   
#include <sys/socket.h>   
#include <netinet/in.h>   

//char * host_name = "127.0.0.1"; // local host   
char * host_name = "10.16.15.124"; // local host   
int port = 9457;   

int
main(int argc, char *argv[]) {   
    char buf[8192];   
    char message[256];   
    int socket_descriptor;   
    struct sockaddr_in pin;   
    struct hostent *server_host_name;   

    int sendlen = 0;
    char * str = "qihoo360\n";   
    int flags;

/*    if ((server_host_name = gethostbyname(host_name)) == 0) {   */
/*        perror("Error resolving local host\n");   */
/*        exit(1);   */
/*    }   */

    bzero(&pin, sizeof(pin));   
    pin.sin_family = AF_INET;   
/*    pin.sin_addr.s_addr = htonl(INADDR_ANY);   */
/*    pin.sin_addr.s_addr = ((struct in_addr *)(server_host_name->h_addr))->s_addr;   */
    pin.sin_addr.s_addr = inet_addr(host_name);
    pin.sin_port = htons(port);   

    if ((socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) == -1) {   
        perror("Error opening socket\n");   
        exit(1);   
    }   

/*    if ((flags = fcntl(socket_descriptor, F_GETFL, 0)) < 0 ||*/
/*            fcntl(socket_descriptor, F_SETFL, flags | O_NONBLOCK) < 0) {*/
/*        perror("setting O_NONBLOCK");*/
/*        close(socket_descriptor);*/
/*        return -1;*/
/*    }*/

    if (connect(socket_descriptor, (void *)&pin, sizeof(pin)) == -1) {   
        perror("Error connecting to socket\n");   
        exit(1);   
    }   

    printf("Sending message %s to server...\n", str);   

    sendlen = send(socket_descriptor,str,strlen(str),0 );
    if (sendlen == -1) {   
        perror("Error in send\n");   
        exit(1);   
    }   

    printf("..sent message.. wait for response... %d \n", sendlen);   

    sendlen = recv(socket_descriptor,buf,8192, 0);
    printf("\nResponse from server:%d\n\n%s\n",sendlen, buf);   

    if (sendlen == -1) {   
        perror("Error in receiving response from server\n");   
        exit(1);   
    }   

    printf("\nResponse from server:\n\n%s\n", buf);   

    close(socket_descriptor);   

    return 1;
}  
